package com.softeem.gulimail.product.web;

import com.softeem.gulimail.product.entity.CategoryEntity;
import com.softeem.gulimail.product.service.CategoryService;
import com.softeem.gulimail.product.vo.Catalog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;



@Controller
public class IndexController {
    @Autowired
    private RedissonClient redisson;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private StringRedisTemplate template;
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        /**
         * TODO 1.查询一级分类
         * Springboot视图解析器的默认前缀为：classpath:/templates/
         * 默认的后缀为: .html
         */
        System.out.println(Thread.currentThread().getId());
        List<CategoryEntity> list = categoryService.getLevelCategory();
        model.addAttribute("categorys",list);
        return "index";
    }
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson(){
        Map<String, List<Catalog2Vo>> map = categoryService.getCatelogJson();
        return map;
    }
    @ResponseBody
    @RequestMapping("/say")
    public String sayHello(){
        RLock lock = redisson.getLock("my-lock");
        lock.lock();//不指定锁的超时时间：1.项目没有执行完，自动续期，续期时间为看门狗的三分之一时间10s  2.业务只要完成，就算宕机，也会释放锁，默认30s
//        lock.lock(10, TimeUnit.SECONDS);  设置锁的超时时间，手动设置了之后看门狗就不会给锁设置自动续期，到期自动解锁
        try {
            System.out.println("正在执行业务"+Thread.currentThread().getId());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println("释放锁。。。"+Thread.currentThread().getId());
            lock.unlock();
        }

        return "hello";
    }
    @ResponseBody
    @RequestMapping("/write")
    public String writeLock(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        String msg = "";
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        try{
             msg = UUID.randomUUID().toString();
            Thread.sleep(10000);
            template.opsForValue().set("RWValue",msg);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/read")
    public String readLock(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        String rwValue = (String) template.opsForValue().get("RWValue");
        return rwValue;
    }
}
